var express = require('express');
var router = express.Router();
var qiniu = require('qiniu');
var superagent = require('superagent');
var async = require( 'async'); // 同步控制
var moment = require('moment');  // 日期格式化


// 分词模块
var Segment = require('node-segment').Segment;// 载入分词模块
var POSTAG = require('node-segment').POSTAG;// 载入词性接口
var segment = new Segment();// 创建实例
segment.useDefault();// 使用默认的识别模块及字典，载入字典文件需要1秒，仅初始化时执行一次即可

var SJGNode = require('./../../sjg/common/tg/SJGNode');
var SJGTG = require('./../../sjg/common/tg/SJGTG');
var SJGTGV3 = require('./../../sjg/common/tg3/SJGTGV3');
var SJGNodeV3 = require('./../../sjg/common/tg3/SJGNodeV3');
var GlobalData = require('./../../sjg/z/GlobalData');
var SJGPage = require('./../../sjg/kernel/cont/SJGPage');

var CONFIG = require('../../../common/config');
var CODE = require('../../../common/code');
var payUtil = require('./payUtil');

var BASE_IP = CONFIG.BASE_IP;

var ParserClassLoader = require('./../../sjg/kernel/base/ParserClassLoader');
ParserClassLoader.loadKernelClass();// 加载类加载器

var debugUtil = (function () {
    function debugUtil() {
    }

    /**修正模板的提纲树
     * 参数: data	模版的提纲树的Json
     * 参数: pages	数组类型，数组元素为SJGPage
     * 参数: ver 版本号
     * 返回结果: 修正后的模版的提纲树的Json
     * */
    debugUtil.fixContentOfTgTree = function(data, pages, ver){
        var tree = null;
        if(ver<SJGNode.VERSION_3){
            tree =new SJGTG();
            tree.fromJson(JSON.parse(data),ver);
        }else{
            tree =new SJGTGV3();
            tree.fromJson(JSON.parse(data),ver);
        }
        for(var i=0;i<pages.length;i++){
            var sjgPg = pages[i];
            if(sjgPg){
                var rs = null;
                if(ver<SJGNode.VERSION_3){
                    rs = sjgPg.replaces;
                }else{
                    rs = sjgPg.replaces2;
                }
                // 更新文档提纲树中的value
                for(var n=0;n<rs.length;n++) {
                    var one = rs[n];
                    if(one ){
                        var f = tree.root.getNodeById(one.id);
                        if(f){
                            var o = null;
                            if(ver<SJGNode.VERSION_3){
                                o = sjgPg.root.getChildById(one.obj_id);
                            }else{
                                for(var t=0;t<one.relations.length;t++){
                                    var rel = one.relations[t];
                                    if(rel && rel.page_index==i){
                                        o = sjgPg.root.getChildById(rel.obj_id);
                                        break;
                                    }
                                }
                            }
                            if(!o) continue;
                            // 分类型处理
                            if(f.type == SJGNode.SJGNODE_TYPE_1){
                                // 文字
                                f.value.value = o.te;
                                f.value.max = o.te.length>f.value.max?o.te.length:f.value.max;

                            }else if(f.type == SJGNode.SJGNODE_TYPE_2){
                                // 图片
                                f.value.value = o.source;
                                f.value.pos = o.image; // 注意这里：提纲图片节点的pos指的是图片的真实宽高，与文档树中的图片对象image含义相同
                                f.value.mask = o.mask;
                            }

                        }
                    }
                }
            }
        }
        return tree.toJson();
    }



    //------------------------------------------------------------------
    return debugUtil;
})();
//------------------------------------------------------------------
module.exports = debugUtil;



